package engine.logic.loading.effect
{
	import flash.display.Shape;
	import flash.events.Event;
	
	import newx.StageInfo;

	/**
	 *百叶窗特效 
	 * @author test
	 * 
	 */	
	public class HorizontalBlinds extends AbstractTransitionEffect
	{
		private static const EffectInOver:int = 0;
		private static const EffectOutOver:int = 1;
		//遮黑，恢复
		private var _type:int;
		//遮罩
		private var _blinds:Shape;
		//当前stage的宽高
		private var _w:int;
		private var _h:int;
		//累加
		private var _plus:int;
		//速度
		private var _speed:int = 5;
		//几条线
		private var _blindCount:int = 8;
		
		public function HorizontalBlinds()
		{
			super();
			_blinds = new Shape();
			addChild(_blinds);
		}
		
		override public function effectIn():void
		{
			_type = EffectInOver;
			_w = StageInfo.stage.stageWidth;
			_h = StageInfo.stage.stageHeight;
			_plus = 0;
			_blinds.graphics.clear();
			_blinds.graphics.beginFill(0x000000);
			
			addEventListener(Event.ENTER_FRAME, drawBlind);
		}
		
		override public function effectOut():void
		{
			_type = EffectOutOver;
			_w = StageInfo.stage.stageWidth;
			_h = StageInfo.stage.stageHeight;
			_plus = 0;
			_blinds.graphics.clear();		
			_blinds.graphics.beginFill(0x000000);
			_blinds.graphics.drawRect(0,0,_w,_h);
			
			addEventListener(Event.ENTER_FRAME, drawBlind);
		}
		
		override public function set width(value:Number):void
		{
			_w = value;
		}
		
		override public function set height(value:Number):void
		{
			_h = value;
		}
		
		private function drawBlind(e:Event):void
		{
			for(var i:int=0; i<_blindCount; ++i)
			{
				//先这样，以后有更好的办法再改
				_blinds.graphics.drawRect(0,i*_h/_blindCount+_plus*_speed,_w,_speed);
			}
			
			_plus++;
			
			if(_plus > _h/_blindCount/_speed - 1)
			{
				removeEventListener(Event.ENTER_FRAME, drawBlind);			
				_blinds.graphics.endFill();
				
				if(_type == EffectInOver)
				{
					dispatchEvent(new TransitionEffectEvent(TransitionEffectEvent.EffectInOver));					
				}
				else if(_type == EffectOutOver)
				{
					_blinds.graphics.clear();
					
					dispatchEvent(new TransitionEffectEvent(TransitionEffectEvent.EffectOutOver));
				}
			}
		}
		
	}
}